import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_chip/bean/Address.dart';
import 'package:flutter_chip/bean/User.dart';
import 'package:flutter_chip/view/forum/view_addforum.dart';
import 'package:flutter_chip/view/forum/view_foruminfo.dart';
import 'package:flutter_chip/view/forum/view_share.dart';
import 'package:flutter_chip/view/goods/order/view_orderinfo.dart';
import 'package:flutter_chip/view/goods/view_addgoods.dart';
import 'package:flutter_chip/view/goods/view_goodsinfo.dart';
import 'package:flutter_chip/view/home/blue/view_blue.dart';
import 'package:flutter_chip/view/home/serial/view_serial.dart';
import 'package:flutter_chip/view/home/view_calculator.dart';
import 'package:flutter_chip/view/home/view_cprogram.dart';
import 'package:flutter_chip/view/home/view_mcu.dart';
import 'package:flutter_chip/view/home/wifi/view_wifi.dart';
import 'package:flutter_chip/view/home/view_led.dart';
import 'package:flutter_chip/view/mine/setting/view_address.dart';
import 'package:flutter_chip/view/mine/setting/view_edit_adrr.dart';
import 'package:flutter_chip/view/mine/setting/view_edit_user.dart';
import 'package:flutter_chip/view/mine/setting/view_input_edit.dart';
import 'package:flutter_chip/view/mine/setting/view_setting.dart';
import 'package:flutter_chip/view/mine/view_about.dart';
import 'package:flutter_chip/view/mine/view_advise.dart';
import 'package:flutter_chip/view/mine/view_member.dart';
import 'package:flutter_chip/view/mine/view_myorder.dart';
import 'package:flutter_chip/view/mine/view_qrcode.dart';
import 'package:flutter_chip/view/mine/view_userinfo.dart';
import 'package:flutter_chip/view/mine/view_userlist.dart';
import 'package:flutter_chip/view/mine/wallet/view_tradeinfo.dart';
import 'package:flutter_chip/view/mine/wallet/view_wallet.dart';
import 'package:flutter_chip/view/news/view_chat.dart';
import 'package:flutter_chip/view/view_checkphone.dart';
import 'package:flutter_chip/view/view_login.dart';
import 'package:flutter_chip/view/view_register.dart';
import 'package:flutter_chip/view/view_setpwd.dart';
import 'package:provide/provide.dart';

import '../main.dart';
import 'Comm.dart';

class Routers {
  static Router router;

  static const String PAGE_MAIN = "/";
  static const String PAGE_LOGIN = "/login";
  static const String PAGE_REGIST = "/register";
  static const String PAGE_CHECKPHONE = "/checkphone";
  static const String PAGE_SETPWD = "/setpwd";
  static const String PAGE_USERINFO = "/userinfo";

  static const String PAGE_MINE_EDIT = "/mine/edit";
  static const String PAGE_MINE_SETTING = "/mine/setting";
  static const String PAGE_USER_LIST = "/mine/userlist";
  static const String PAGE_ABOUT = "mine/about";
  static const String PAGE_QRCODE = "mine/qrcode";
  static const String PAGE_ADVISE = "/mine/advise";
  static const String PAGE_WALLET = "/mine/wallet";
  static const String PAGE_MEMBER = "/mine/member";
  static const String PAGE_ORDER = "/mine/order";

  static const String PAGE_HOME_CMCU = "/course/mcu";
  static const String PAGE_HOME_CC = "/course/c";
  static const String PAGE_HOME_BLUE = "/home/blue";
  static const String PAGE_HOME_WIFI = "/home/wifi";
  static const String PAGE_HOME_LED = "/home/led";
  static const String PAGE_HOME_SERIAL = "/home/serial";
  static const String PAGE_HOME_CALCULATOR = "/home/calculator";

  static const String PAGE_GOOD_INFO = '/goods/info';
  static const String PAGE_ADDGOODS = '/goods/add';

  static const String PAGE_ORDERINFO = '/order/info';

  static const String PAGE_ADDFORUM = '/forum/add';
  static const String PAGE_FORUMINFO = '/forum/info';
  static const String PAGE_SHARE = '/forum/share';

  static const String PAGE_INPUT = "/comm/edit";

  static const String PAGE_CHAT = "/news/chat";
  static const String PAGE_TRADE = "/wallet/trade";

  static const String PAGE_ADDRESS = "/setting/address";
  static const String PAGE_ADDADRS = "/setting/addadrs";

  static void configureRoutes(Router router) {
    router.define(PAGE_MAIN,
        handler: Handler(handlerFunc: (context, params) => MyHomePage()));
    router.define(PAGE_LOGIN,
        handler: Handler(handlerFunc: (context, params) => LoginPage()));
    router.define(PAGE_REGIST, handler: Handler(handlerFunc: (context, params) {
      var phone = params['message']?.first; //取出传参
      return RegisterPage(phone: phone);
    }));

    router.define(PAGE_CHECKPHONE,
        handler: Handler(handlerFunc: (context, params) {
      var title = params['title']?.first;
      var phone = params['phone']?.first;
      debugPrint("$phone $title");
      return CheckPhonePage(title: title, phone: phone);
    }));

    router.define(PAGE_SETPWD, handler: Handler(handlerFunc: (context, params) {
      var phone = params['phone']?.first; //取出传参
      var type = params['type']?.first;
      debugPrint("$phone $type");
      return SetPwdPage(phone, type: type);
    }));

    ///用户详情
    router.define(PAGE_USERINFO,
        handler: Handler(handlerFunc: (context, params) {
      var userId = params['userId']?.first;
      User user = Provide.value<User>(context);
      print("当前用户：${user.toJson()}  userId=$userId");
      return UserInfoPage(userId, null != user && user.userId == userId);
    }));

    ///编辑资料
    router.define(PAGE_MINE_EDIT,
        handler: Handler(handlerFunc: (context, params) {
      return EditUserPage();
    }));

    ///设置页面
    router.define(PAGE_MINE_SETTING,
        handler: Handler(handlerFunc: (context, params) {
      return SettingPage();
    }));

    ///关于我们
    router.define(PAGE_ABOUT, handler: Handler(handlerFunc: (context, params) {
      return AboutPage();
    }));

    ///我的名片
    router.define(PAGE_QRCODE, handler: Handler(handlerFunc: (context, params) {
      User user = Provide.value<User>(context);
      if (null == user) return null;
      var sellerNum = params['sellerNum']?.first;
      return QrCodePage(user, sellerNum);
    }));

    ///MCU
    router.define(PAGE_HOME_CMCU,
        handler: Handler(handlerFunc: (context, params) {
      var type = params['type']?.first;
      return McuPage(Comm.courseMap[type], type);
    }));

    ///C
    router.define(PAGE_HOME_CC,
        handler: Handler(handlerFunc: (context, params) {
      var type = params['type']?.first;
      return CProgramPage(Comm.courseMap[type], type);
    }));

    ///BLUE
    router.define(PAGE_HOME_BLUE,
        handler: Handler(handlerFunc: (context, params) {
      return BluePage();
    }));

    ///wifi串口
    router.define(PAGE_HOME_WIFI,
        handler: Handler(handlerFunc: (context, params) {
      return WifiPage();
    }));

    ///wifi串口
    router.define(PAGE_HOME_LED,
        handler: Handler(handlerFunc: (context, params) {
      return LedPage();
    }));

    ///wifi串口
    router.define(PAGE_HOME_SERIAL,
        handler: Handler(handlerFunc: (context, params) {
      return SerialPage();
    }));

    ///进制计算器
    router.define(PAGE_HOME_CALCULATOR,
        handler: Handler(handlerFunc: (context, params) {
      return CalculatorPage();
    }));

    ///添加商品
    router.define(PAGE_ADDGOODS,
        handler: Handler(handlerFunc: (context, params) {
      String goodsId = params["goodsId"]?.first;
      return AddGoodsPage(goodsId: goodsId);
    }));

    ///商品详情
    router.define(PAGE_GOOD_INFO,
        handler: Handler(handlerFunc: (context, params) {
      var data = params['goodsId']?.first;
      return GoodsInfoPage(goodsId: data);
    }));

    ///论坛发帖
    router.define(PAGE_ADDFORUM,
        handler: Handler(handlerFunc: (context, params) {
      return AddForumPage();
    }));

    ///话题详情
    router.define(PAGE_FORUMINFO,
        handler: Handler(handlerFunc: (context, params) {
      String topicId = params["topicId"]?.first;
      print('打开话题详情-->参数：$topicId');
      return ForumInfoPage(topicId);
    }));

    ///文章分享
    router.define(PAGE_SHARE, handler: Handler(handlerFunc: (context, params) {
      return ShareArticle();
    }));

    ///粉丝列表
    router.define(PAGE_USER_LIST,
        handler: Handler(handlerFunc: (context, params) {
      var type = int.parse(params['type']?.first);
      var userId = params['userId']?.first;
      User user = Provide.value<User>(context);
      return UserListPage(type, userId, null != user && userId == user.userId);
    }));

    ///编辑的输入框
    router.define(PAGE_INPUT, handler: Handler(handlerFunc: (context, params) {
      return InputPage();
    }));

    ///聊天页面
    router.define(PAGE_CHAT, handler: Handler(handlerFunc: (context, params) {
      var data = params['user']?.first;
      var type = params['msgTag']?.first;
      var list = List<int>();

      ///字符串解码
      jsonDecode(data).forEach(list.add);
      final String value = Utf8Decoder().convert(list);
      User user = User.fromJson(json.decode(value));
      return ChatPage(user: user, msgTag: type);
    }));

    ///意见反馈
    router.define(PAGE_ADVISE, handler: Handler(handlerFunc: (context, params) {
      return AdvisePage();
    }));

    ///会员中心
    router.define(PAGE_MEMBER, handler: Handler(handlerFunc: (context, params) {
      User user = Provide.value<User>(context);
      return MemberPage(user);
    }));

    ///我的钱包
    router.define(PAGE_WALLET, handler: Handler(handlerFunc: (context, params) {
      return WalletPage();
    }));

    ///订单详情
    router.define(PAGE_ORDERINFO,
        handler: Handler(handlerFunc: (context, params) {
      var orderId = params['orderId']?.first;
      return OrderInfoPage(orderId);
    }));

    ///我买到的&我卖出的
    router.define(PAGE_ORDER, handler: Handler(handlerFunc: (context, params) {
      var type = params['type']?.first;
      return MyOrderPage(type: type);
    }));

    ///交易流水
    router.define(PAGE_TRADE, handler: Handler(handlerFunc: (context, params) {
      var dirType = params['dirType']?.first;
      return TradeInfoPage(dirType: dirType);
    }));

    ///地址列表
    router.define(PAGE_ADDRESS,
        handler: Handler(handlerFunc: (context, params) {
      return AddressPage();
    }));

    ///新增地址
    router.define(PAGE_ADDADRS,
        handler: Handler(handlerFunc: (context, params) {
      Address address;
      if (params.containsKey("address")) {
        var data = params['address']?.first;
        var list = List<int>();

        ///字符串解码
        jsonDecode(data).forEach(list.add);
        final String value = Utf8Decoder().convert(list);
        address = Address.fromJson(json.decode(value));
      }
      return EditAddress(address);
    }));

    Routers.router = router;
  }
}
